package strangeattractor.core;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;

public class ZPixelGrid {
	//A pixel grid with a z-buffer
	//A pixel will only be overwritten if its Z is smaller than the current Z
	
	private BufferedImage pixels;
	private double[][] zvalues;
	private int width, height;
	public ZPixelGrid(int width, int height) {
		super();
		this.width = width;
		this.height = height;
		this.pixels = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		this.zvalues = new double[width][height];
		for(int x = 0; x < width; x++){
			for(int y = 0; y < height; y++){
				pixels.setRGB(x, y, Color.black.getRGB());
			}
		}
	}
	
	public Color getColor(int x, int y){
		return Color.decode(""+pixels.getRGB(x, y));
		
		
	}
	
	public Image getImage(){
		return pixels;
	}
	public void setPixel(boolean override, Color c, double zScore, Point p){
		try{
			if(override || (zScore < zvalues[p.x][p.y] || zvalues[p.x][p.y] == 0)){
				pixels.setRGB(p.x, p.y, c.getRGB());
				zvalues[p.x][p.y] = zScore;
			}
		}
		catch(ArrayIndexOutOfBoundsException e){}
	}
	
}
